<template>
	<div class="pageSetting">
		<shyt-table
			ref="shytTable"
			:columns="columns"
			:data="tableData"
			:auto-resize="true"
			:keep-source="true"
			:tree-config="treeConfig"
			:toolbar-config="toolbarConfig"
			:edit-config="{ trigger: 'dblclick', mode: 'row', showStatus: true }"
		>
			<template #orgName="{ row }">
				<span>{{ row.orgName }}</span>
			</template>
			<template #whetherEnable="{ row }">
				<span>{{ whetherEnableObject[row.whetherEnable] }}</span>
			</template>
			<template #remark="{ row }">
				<span>{{ row.remarks }}</span>
			</template>
			<template #toolbar_buttons>
				<el-input
					v-model="query"
					placeholder="请输入名称或拼音码"
					size="small"
					style="width: 200px"
					clearable
					@keyup.enter.native="handlerSearch"
				>
					<el-button
						slot="append"
						icon="el-icon-search"
						@click="handlerSearch"
					/>
				</el-input>
				<el-button type="primary" size="small" @click="handlerAddClick()">
					新增
				</el-button>
			</template>
			<template #edit="{ row }">
				<el-button type="text" size="small" @click="handlerEditRow(row)">
					编辑
				</el-button>
				<el-button type="text" size="small" @click="handlerEditAddOrg(row)">
					添加
				</el-button>
				<el-button type="text" size="small" @click="handlerDelRow(row)">
					删除
				</el-button>
			</template>
		</shyt-table>
		<orgDialog
			ref="orgDialog"
			:visible.sync="orgDialogVisible"
			:title="orgDialogTitle"
			:org-data.sync="curOrgData"
			:org-select.sync="curOrgSelect"
			:parent-org.sync="parentOrg"
			:org-dialog-type="orgDialogType"
			:cur-select="curSelect"
			:row-repetition="rowRepetition"
			@submit="handlerSubmit"
		/>
	</div>
</template>

<script>
import {
	orgInsert,
	deleteOrg,
	getOrgTreeList,
	updateOrg,
	findVOByVO,
	getModel,
	// getList,
} from '@/http/org'
import orgDialog from './components/orgDialog'
export default {
	name: 'OrgSetting',
	components: {
		orgDialog,
	},
	data() {
		return {
			query: '',
			orgDialogVisible: false,
			whetherEnableObject: { 0: '是', 1: '否' },
			orgDialogTitle: '',
			orgDialogType: '',
			columns: [
				{
					field: 'orgName',
					title: '机构名称',
					slots: { default: 'orgName' },
					treeNode: true,
				},
				{
					field: 'whetherEnable',
					title: '是否启用',
					slots: { default: 'whetherEnable' },
				},
				{
					field: 'remark',
					slots: { default: 'remark' },
					title: '备注',
				},
				{
					title: '操作',
					slots: { default: 'edit' },
				},
			],
			treeConfig: {
				children: 'children',
				expandAll: true,
			},
			tableData: [],
			$table: null,
			toolbarConfig: {
				slots: {
					buttons: 'toolbar_buttons',
				},
			},
			curOrgData: {},
			curOrgSelect: {},
			parentOrg: {},
			curSelect: '',
			rowRepetition: '',
		}
	},
	computed: {},
	watch: {},
	mounted() {
		this.handlerSearch()
		this.$table = this.$refs.shytTable.$refs.xTable
	},
	methods: {
		handlerSearch() {
			this.getOrgTreeList({
				orgNameOrInputCode: this.query.toUpperCase(),
			})
		},
		getOrgTreeList(param) {
			getOrgTreeList(param).then(({ data }) => {
				if (data?.head?.code == 200) {
					this.tableData = data.data
					this.$nextTick(() => {
						this.$table.setAllTreeExpand(true)
					})
				}
			})
		},
		handlerSubmit(formData, parentData, type) {
			let res = { ...formData }
			if (type == '1') {
				if (!parentData?.id) {
					res.parentUuid = 0
				} else {
					res.parentUuid = parentData.id
				}
				orgInsert(res).then(({ data }) => {
					if (data?.head?.code == 200) {
						this.$message.success('新增成功')
						this.orgDialogVisible = false
						this.handlerSearch()
					}
				})
			} else {
				updateOrg(res).then(({ data }) => {
					if (data?.head?.code == 200) {
						this.$message.success('编辑成功')
						this.orgDialogVisible = false
						this.handlerSearch()
					}
				})
			}
		},
		handlerAddClick(row) {
			this.rowRepetition = ''
			this.orgDialogTitle = '机构新增'
			this.orgDialogType = '1'
			let officeType_num = '0'
			if (row) {
				if (!row.officeType || row.officeType == '0') {
					officeType_num = '1'
				}
				if (row.officeType == '1') {
					officeType_num = '2'
				}
			}
			this.curOrgData = {
				ownerShip: '0', //所有制形式
				orgLevel: '', //机构级别
				orgOrder: '', //机构等次
				orgType: '', //机构类别
				deptType: '', //科室类别
				isIcuFlag: '',
				officeType: officeType_num,
				primaryOrgCode: '',
				primaryOrgName: '',
				orgName: '',
				orgCode: '',
				remark: '',
				whetherEnable: '0',
				admOperatNumber: '',
				approvedBedsNumber: '',
				openBedsNumber: '',
				patentNumber: '',
				admDiagnosisNumber: '',
				deptBeds: '',
				deptOpenBeds: '',
				changeBedsNumber: '',
				doctorNumber: '',
				nurseNumber: '',
				regularDoctorNumber: '',
				regularNurseNumber: '',
				wardNumber: '',
				operateRoomNum: '',
				parentUuid: '0',
				deptBelongingType: '',
				isRegister: '',
			}
			this.orgDialogVisible = true
		},
		handlerEditRow(row) {
			this.rowRepetition = row.id
			this.orgDialogTitle = '机构编辑'
			this.orgDialogType = '2'
			findVOByVO({ id: row.id }).then(({ data }) => {
				this.curOrgData.parentUuid = row.id
				this.curOrgData = data.data
			})
			this.orgDialogVisible = true
		},
		handlerDelRow(row) {
			let msg =
				row.children && row.children.length > 0
					? '此操作将连带删除子机构，是否继续?'
					: '此操作将删除该机构, 是否继续?'
			this.$confirm(msg, '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(() => {
					deleteOrg({ id: row.id }).then(({ data }) => {
						if (data?.head?.code == 200) {
							this.$message.success('删除成功')
							this.handlerSearch()
						}
					})
				})
				.catch(() => {})
		},
		handlerEditAddOrg(row) {
			this.rowRepetition = row.id
			this.curSelect = ''
			this.parentOrg = row
			this.handlerAddClick(row)
			getModel({ id: row.id }).then(({ data }) => {
				if (data.data.officeType == '0') {
					this.curSelect = data.data.officeType
				} else {
					this.curOrgData.officeType = 2
					this.curSelect = '-1'
				}
			})
		},
	},
}
</script>

<style lang="scss" scoped>
.pageSetting {
	padding: 10px;
	box-sizing: border-box;
	height: 100%;
}
</style>
